var FBook = {};

var isFaceBook =true;
var imgUrl = "resource/icon.png";
FBook.getInfoType = function (type,backfun) {
    if (isFaceBook) {
        switch (type) {
            case "getName":
                return getName();
                break;
            case "getID":
                return getID();
                break;
            case "getPhoto":
                return getPhoto(backfun);
                break;
            case "getLocale":
                return getLocale();
                break;
            case "getUserInfo":
                return getUserInfo();
                break;
            case "setRankInfoForFaceBook":
                return setRankInfoForFaceBook();
                break;
            case "getMySelfRankInfoForFaceBook":
                return getMySelfRankInfoForFaceBook(backfun);
                break;
            case "getAllRankInfoForFaceBook":
                return getAllRankInfoForFaceBook();
                break;
        }
    } else {
        return null;
    }

};

function getName() {
    const playerName = FBInstant.player.getName();
    return playerName;
};

function getID(count) {
    const userID = FBInstant.player.getID();
    return userID;

};

function getPhoto(backfun) {

    // backfun("sssss");
    const playerPic = FBInstant.player.getPhoto();
    backfun(playerPic);
    // return playerPic;
};

function getLocale() {
    const playerLocal = FBInstant.getLocale();
    return playerLocal;
};

//设置排行榜的数据  //可以往里面传参数
function setRankInfoForFaceBook(score) {
    console.log("FaceBookUtils--->setRankInfoForFaceBook--->uploading Score ！！！", score)
    if (typeof FBInstant === 'undefined') return;
    score = (score).toFixed(0);
    score = Number(score);
    // console.log("给facebook上传自己的分数  排行榜使用",score)
    FBInstant.getLeaderboardAsync(worldRank)    //通过名字 回去排行榜
        .then(function (leaderboard) {
            // console.log("leaderboard.name",leaderboard.getName());
            return leaderboard.setScoreAsync(score);
        })
        .then(() => LogUtils.log('Score saved'))
        .catch(function (error) {
            // console.log("error",error)
        });
};

//获取玩家自己的排行榜信息
function getMySelfRankInfoForFaceBook(backFun) {
    var worldRank = "rank";
    if (typeof FBInstant === 'undefined') return;
    FBInstant.getLeaderboardAsync(worldRank)
        .then(leaderboard => leaderboard.getPlayerEntryAsync())
        .then(entry => {
            console.log("FaceBookUtils--->getMySelfRankInfoForFaceBook--->entry", entry);
            if (entry && entry.getRank) {
                var myIndex = entry.getRank();
                console.log("FaceBookUtils--->getMySelfRankInfoForFaceBook--->myIndex", myIndex);
                if (backFun) {
                    backFun(myIndex)
                }
            } else {
                console.log("entry.getRank error！！！")
            }

        }).catch(error => console.error(error));
};

//获取全部的排行榜信息
function getAllRankInfoForFaceBook(backFun) {
    if (typeof FBInstant === 'undefined') return;
    FBInstant.getLeaderboardAsync(worldRank)
        .then(leaderboard => leaderboard.getEntriesAsync(10, 0))
        .then(entries => {
            console.log("entries", entries);
            if (backFun) {
                backFun(entries)
            }
        }).catch(error => console.error(error));
};

function getUserInfo() {
    var userInfo = {};
    userInfo.playerName = FBInstant.player.getName();
    userInfo.userID = FBInstant.player.getID();
    userInfo.playerPic = FBInstant.player.getPhoto();
    userInfo.playerLocal = FBInstant.getLocale();
    return userInfo;
};

module.exports = FBook;